package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.VisitTypeResponse;

import java.util.ArrayList;


public class VisitypeAdapter extends RecyclerView.Adapter<VisitypeAdapter.ListItemViewHolder> {

  private static final String TAG = "VisitypeAdapter:";
  private Context mConext;
  private ArrayList<VisitTypeResponse.Data> mList;
  private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
  private boolean mIsSelectable = false;


     public VisitypeAdapter(Context mConext,ArrayList<VisitTypeResponse.Data> list) {
        this.mConext = mConext;
        if (list == null) {
        throw new IllegalArgumentException("model Data must not be null");
        }
          mList = list;
        }

      //更新adpter的数据和选择状态
    public void updateDataSet(ArrayList<VisitTypeResponse.Data> list) {
        this.mList = list;
        mSelectedPositions = new SparseBooleanArray();

        }


      //获得选中条目的结果
    public ArrayList<VisitTypeResponse.Data> getSelectedItem() {

        ArrayList<VisitTypeResponse.Data> selectList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
        if (isItemChecked(i)) {

          selectList.add(mList.get(i));
         }
        }
        return selectList;

        }


@Override
      public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.visit_type_item, viewGroup, false);
        return new ListItemViewHolder(itemView);
        }

     //设置给定位置条目的选择状态
      private void setItemChecked(int position, boolean isChecked) {

            mSelectedPositions.put(position, isChecked);
        }

     //根据位置判断条目是否选中
       private boolean isItemChecked(int position) {

        return mSelectedPositions.get(position);
        }

       //根据位置判断条目是否可选
       private boolean isSelectable() {

        return mIsSelectable;
        }
       //设置给定位置条目的可选与否的状态
       private void setSelectable(boolean selectable) {

        mIsSelectable = selectable;
        }

     //绑定界面，设置监听
       @Override
       public void onBindViewHolder(final ListItemViewHolder holder, final int i) {
        //设置条目状态
         holder.tv_productName.setText(mList.get(i).getLabel());

           if (!isItemChecked(i)){

               holder.tv_productName.setTextColor(mConext.getResources().getColor(R.color.select_visit_normal));
               holder.tv_productName.setBackgroundResource(R.drawable.noselected_visti_type);

           }else {

               holder.tv_productName.setTextColor(mConext.getResources().getColor(R.color.home_bottom_text_checked));
               holder.tv_productName.setBackgroundResource(R.drawable.select_visit_type);

           }
           holder.tv_productName.setPadding(10,0,10,0);


        //条目view的监听
        holder.itemView.setOnClickListener(v -> {
        if (isItemChecked(i)) {

             setItemChecked(i, false);
             holder.tv_productName.setTextColor(mConext.getResources().getColor(R.color.select_visit_normal));
             holder.tv_productName.setBackgroundResource(R.drawable.noselected_visti_type);
             holder.tv_productName.setPadding(10,0,10,0);

        } else {

              setItemChecked(i, true);
             holder.tv_productName.setTextColor(mConext.getResources().getColor(R.color.home_bottom_text_checked));
              holder.tv_productName.setBackgroundResource(R.drawable.selected_visit_type);
             holder.tv_productName.setPadding(10,0,10,0);

        }
//        notifyItemChanged(i);
            if (null != itemClickListener){

                int count = getSelectedItem().size();
                itemClickListener.onClick(holder.itemView ,i,count);
            }


        });

        }

       @Override
       public int getItemCount() {
        return mList == null ? 0 : mList.size();
        }

     public class ListItemViewHolder extends RecyclerView.ViewHolder{

         //ViewHolder
      TextView tv_productName;

       ListItemViewHolder(View view) {
        super(view);
        this.tv_productName = view.findViewById(R.id.tv_visittype);

      }

 }

 private OnItemClickListener itemClickListener;

  public void setOnItemClickListener(OnItemClickListener itemClickListener){

      this.itemClickListener = itemClickListener;
  }

 public interface  OnItemClickListener{

        void onClick(View v,int position,int count);
    }
}


